package UPPM;

import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import UPPM.Utils;

public class TestCueSheets {
	
	WebDriver driver = new FirefoxDriver();

	@Before
	public void setUp() throws Exception {
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		driver.get("http://test.unippmtest.co.uk");
		}


	@Test
    public void testCreateNewCueSheet() throws Exception {
		
		Utils.Login(driver);
		Utils.waitForElement(driver, By.xpath("//*[@id='outline']/div/div[2]/div[3]/div[1]/ul/li[6]/a/span/span"));
		WebElement cue_sheets_tab = driver.findElement(By.xpath("//*[@id='outline']/div/div[2]/div[3]/div[1]/ul/li[6]/a/span/span"));
		cue_sheets_tab.click();
		
		Utils.waitForElement(driver, By.xpath("//*[@id='outline']/div/div[4]/div/div[1]/div/div[2]/div/div[1]/div[2]/a/span"));
		WebElement new_cuesh_btn = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div/div[1]/div/div[2]/div/div[1]/div[2]/a"));
		new_cuesh_btn.click();
		
	    //STEP 1 OF 5: cue Sheet Details
		Utils.waitForElement(driver, By.id("next_btn"));
		WebElement prod_name = driver.findElement(By.id("production_name"));
		Assert.assertEquals(true, prod_name.getText().equals("Production Name"));
		WebElement prod_name_input = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[1]/div[1]/input"));
		prod_name_input.sendKeys("test");
		WebElement prod_date = driver.findElement(By.id("production_date"));
		Assert.assertEquals(true, prod_date.getText().equals("Production Date"));
		WebElement created_by = driver.findElement(By.id("created_by"));
		Assert.assertEquals(true, created_by.getText().equals("Created By"));
		//need to check the other 2 fields too
		WebElement cancel_btn = driver.findElement(By.id("cancel_btn"));
		Assert.assertEquals(true, cancel_btn.isEnabled());
		WebElement next_btn = driver.findElement(By.id("next_btn"));
		next_btn.click();
		
		//STEP 2 OF 5: Cue Sheet Tracks
		Utils.waitForElement(driver, By.id("back_btn"));
		//add from proj section
		WebElement add_from_proj_btn = driver.findElement(By.id("btn_addtrack_project"));
		Assert.assertEquals(true, add_from_proj_btn.isDisplayed());
		//add from search
		WebElement add_from_search = driver.findElement(By.id("btn_addtrack_predicted"));
		Assert.assertEquals(true, add_from_search.isDisplayed());
		//add from library
		WebElement add_from_lib = driver.findElement(By.id("btn_addtrack_lable"));
		Assert.assertEquals(true, add_from_lib.isDisplayed());
		//add non uppm music
		WebElement track_title = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[6]/div[1]/div/div[1]/input"));
		track_title.sendKeys("test track");
		WebElement track_no = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[6]/div[1]/div/div[2]/input"));
		track_no.sendKeys("11");
		WebElement cd_no = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[6]/div[1]/div/div[3]/input"));
		cd_no.sendKeys("11");
		WebElement composer = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[6]/div[1]/div/div[4]/input"));
		composer.sendKeys("composer");
		WebElement artist = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[6]/div[1]/div/div[5]/input"));
		artist.sendKeys("artist");
		WebElement publisher = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[6]/div[1]/div/div[6]/input"));
		publisher.sendKeys("publisher");
		WebElement add_non_uppm = driver.findElement(By.id("btn_addtrack_nonuppm"));
		add_non_uppm.click();
		//check buttons and go to next step
		Utils.CueSheetCheckBtns(driver);
		
		//STEP 3 OF 5: 
		Utils.waitForElement(driver, By.id("production_airdate"));
		WebElement prod_tit = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[1]/div[1]/input"));
		prod_tit.sendKeys("production title");
		WebElement prod_no = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[1]/div[2]/input"));
		prod_no.sendKeys("123");
		WebElement ep_tit = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[1]/div[3]/input"));
		ep_tit.sendKeys("episode title 123");
//		WebElement air_date = driver.findElement(By.id("production_airdate"));
//		air_date.click();
		WebElement length = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[1]/div[5]/input"));
		length.sendKeys("123");
		WebElement network = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div[1]/div/div[1]/div[2]/div/div[2]/div[2]/div[1]/div[6]/input"));
		network.sendKeys("123");
		//check buttons and go to next step
		Utils.CueSheetCheckBtns(driver);
		
		//STEP 4 OF 5: Cue Sheet Producer
		Utils.waitForElement(driver, By.id("copy_producer_info"));
		//check all data is displayed correctly
		//check buttons and go to next step
		Utils.CueSheetCheckBtns(driver);

		//STEP 5 OF 5: Review and Submit Cue Sheets
		Utils.waitForElement(driver, By.id("submit_btn"));
		//check btns and submit
		WebElement back_btn = driver.findElement(By.id("back_btn"));
		Assert.assertEquals(true, back_btn.isEnabled());
		WebElement save_btn = driver.findElement(By.id("save_btn"));
		Assert.assertEquals(true, save_btn.isEnabled());
		WebElement export_btn = driver.findElement(By.id("export_btn"));
		Assert.assertEquals(true, export_btn.isEnabled());
		WebElement submit_btn = driver.findElement(By.id("submit_btn"));
		submit_btn.click();
		
		Utils.waitForElement(driver, By.id("confirmation_overlay"));
		WebElement no_btn = driver.findElement(By.id("noConfirmationBtn"));
		Assert.assertEquals(true, no_btn.isEnabled());
		WebElement yes_btn = driver.findElement(By.id("yesConfirmationBtn"));
		yes_btn.click();
		
		//check that cue sheet was created and submitted
		Utils.waitForElement(driver, By.xpath("//*[@id='outline']/div/div[4]/div/div[1]/div/div[2]/div/div[1]/div[2]/a/span"));
	}
	
	
	
	
	
	@Test
    public void testCueSheetsInitialState() throws Exception {
		
		Utils.Login(driver);
		Utils.waitForElement(driver, By.xpath("//*[@id='outline']/div/div[2]/div[3]/div[1]/ul/li[6]/a/span/span"));
		WebElement cue_sheets_tab = driver.findElement(By.xpath("//*[@id='outline']/div/div[2]/div[3]/div[1]/ul/li[6]/a/span/span"));
		cue_sheets_tab.click();
		
		Utils.waitForElement(driver, By.xpath("//*[@id='outline']/div/div[4]/div/div[1]/div/div[2]/div/div[1]/div[2]/a/span"));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath("//*[@id='outline']/div/div[2]/div[3]/div[1]/ul/li[6]/a/span/span")));
		
		//check that filter cue sheets is displayed
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("filter_cuesheets")));
	
		//check cue sheets list table header
		WebElement cue_sh_no = driver.findElement(By.xpath("//*[@id='listingCueSheetDiv']/div[5]/div[1]"));
		Assert.assertEquals(true, cue_sh_no.isDisplayed());
		WebElement prod_tit = driver.findElement(By.xpath("//*[@id='listingCueSheetDiv']/div[5]/div[2]"));
		Assert.assertEquals(true, prod_tit.isDisplayed());
		WebElement status = driver.findElement(By.xpath("//*[@id='listingCueSheetDiv']/div[5]/div[3]"));
		Assert.assertEquals(true, status.isDisplayed());
		WebElement date_created = driver.findElement(By.xpath("//*[@id='listingCueSheetDiv']/div[5]/div[4]"));
		Assert.assertEquals(true, date_created.isDisplayed());
		WebElement prod_no = driver.findElement(By.xpath("//*[@id='listingCueSheetDiv']/div[5]/div[5]"));
		Assert.assertEquals(true, prod_no.isDisplayed());
		WebElement actions = driver.findElement(By.xpath("//*[@id='listingCueSheetDiv']/div[5]/div[6]"));
		Assert.assertEquals(true, actions.isDisplayed());
		
		//check sorting options
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("sort")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("results_num")));
		}
	
	
	@After
	public void tearDown() throws Exception {
		driver.quit();
	}
}

